package com.example.validator;

import com.example.entity.ParkingFeeConfig;

import java.math.BigDecimal;
import java.util.function.Predicate;

/**
 * @author fuhao
 */
public class ParkingFeeValidator {

    private static final Predicate<BigDecimal> NON_NEGATIVE =
            value -> value == null || value.compareTo(BigDecimal.ZERO) >= 0;

    private static final Predicate<Integer> POSITIVE =
            value -> value == null || value > 0;

    public static void validate(ParkingFeeConfig config) {
        validateField(config.getFreeTime(), POSITIVE, "Free time must be positive");
        validateField(config.getChargeMaxAmount(), NON_NEGATIVE, "Max fee cannot be negative");
        // 其他验证
    }

    private static <T> void validateField(T value, Predicate<T> validator, String errorMsg) {
        if (!validator.test(value)) {
            throw new IllegalArgumentException(errorMsg);
        }
    }
}
